package com.example.replacebooks.service;

import cn.hutool.core.date.DateUtil;
import com.example.replacebooks.common.JwtTokenUtils;
import com.example.replacebooks.entity.Bookinfo;
import com.example.replacebooks.entity.Class;
import com.example.replacebooks.entity.Message;
import com.example.replacebooks.entity.Params;
import com.example.replacebooks.exception.CustomException;
import com.example.replacebooks.mapper.BookinfoMapper;
import com.example.replacebooks.mapper.ClassMapper;
import com.example.replacebooks.mapper.MessageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MessageService {
    @Resource
    private MessageMapper messageMapper;

    @Resource
    private BookinfoMapper bookinfoMapper;

    public List<Message> findAll(){
        return messageMapper.selectAll();
    }

    //获取消息
    public PageInfo<Message> findBySearch(Params params){
        Integer bookid = params.getBookid();
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页查询
        List<Message> list = messageMapper.findBySearch(params,bookid);
        return PageInfo.of(list);
    }

    //获取已读消息
    public PageInfo<Message> findBySearchByread(Params params){
        Integer userid = JwtTokenUtils.getCurrentUser().getUserid();
        // 开启分页查询
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        // 接下来的查询会自动按照当前开启的分页查询
        // 根据userid去判断 发布者与被发者
        List<Message> list = messageMapper.findBySearchByread(params,userid);
        return PageInfo.of(list);
    }

    // 未读更新已读
    public void updateByMessageid(Message message) {
        message.setReadstatu("已读");
        messageMapper.updateByMessageid(message);
    }

    // 回复消息添加
    public void replyadd(Message message) {
        // 用传输过来的消息ID 将 状态改为已读
        messageMapper.updateByMessageidToReadstatu(message);
        // 交换发布者与被发者
        Integer userid = message.getUserid();
        Integer touserid = message.getTouserid();
        message.setUserid(touserid);
        message.setTouserid(userid);
        // 去掉消息ID 然后新增
        message.setMessageid(null);
        message.setReadstatu("未读");
        message.setMessagetime(DateUtil.now());
        // 添加
        messageMapper.insertSelective(message);
    }

    // 留言消息添加
    public void leaveadd(Message message) {
        message.setMessagetype("留言");
        message.setMessagetime(DateUtil.now());
        // 通过这本书的bookid去获取发布者的id
        Bookinfo book = bookinfoMapper.findByBookid(message.getBookid());
        message.setTouserid(book.getUserid());
        // 获取登录的userid
        message.setUserid(JwtTokenUtils.getCurrentUser().getUserid());
        // 添加
        messageMapper.insertSelective(message);
    }

    // 评价消息添加
    public void evaluateadd(Message message) {
        message.setMessagetype("评价");
        message.setMessagetime(DateUtil.now());
        // 通过这本书的bookid去获取发布者的id
        Bookinfo book = bookinfoMapper.findByBookid(message.getBookid());
        message.setTouserid(book.getUserid());
        // 获取登录的userid
        message.setUserid(JwtTokenUtils.getCurrentUser().getUserid());
        // 添加
        messageMapper.insertSelective(message);
    }

    public void delete(Integer userid) {
        messageMapper.deleteByPrimaryKey(userid);
    }
}
